﻿package bdz.projects.lavender.doi.ui
{
	import flash.display.MovieClip;
	import flash.utils.*;
	import flash.display.*;
	
	import gs.*;
	import gs.easing.*;
	
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.*;
	import org.papervision3d.objects.primitives.Plane;
	
	/**
	 * ...
	 * @author ...
	 */
	public class DOICategoryPlaneView extends DOIPlane 
	{
		
		private var _padding:Number = 2;
		public var _indelay:Number = 0;
		
		private var _labelPlane:Plane;
		private var _intme:Number;
		private var _basemat:MovieMaterial;
		private var _basemovie:MovieClip;
		public var introPlayed:Boolean = false;
		private var _shownav:Boolean;
		private var _menuhasshown:Boolean = false;
		
		public var planeView:DOIPlanesView;
		public function DOICategoryPlaneView(catname:String, dobj:Object, backed:Number = 0, ishow:Boolean = false) {
		
			
			super(catname, null);
			_defobj = dobj;
			_backed = backed;
			_shownav = ishow;
			
			
		} 
		override protected function renderPlane(delay:Number = 0):void {
			super.renderPlane(_indelay);
			
			
			
			var obj:* = getDefinitionByName(_renderer);
			_basemovie = new obj();
			_mcref.push(_basemovie);
			
			_basemat = new MovieMaterial(_basemovie, false, false, false);
			_basemat.smooth = true;
			_basemat.allowAutoResize = false;
			
			_matref.push(_basemat);
			
			var _labelmc:MovieClip = new CatLabel();
			_mcref.push(_labelmc);
			_basemovie.defmat = _basemat;
			
			var _labelmat:MovieMaterial = new MovieMaterial(_labelmc, true, false, false);
			_labelmat.smooth = true;
			_labelmc.defmat = _labelmat;
			_labelmc.definfo = _defobj.name.split("|").join(" ");
			_labelmat.name = "labelmat";
			
			_matref.push(_labelmat);
			
			_labelPlane = new Plane(_labelmat, _labelmc.width, _labelmc.height, 6, 6);
			_labelPlane.z = -60;
			_labelPlane.x = -125;
			_labelPlane.y = (_backed) ? 120 : 120;
			
			
			_frontContainer.addChild(_labelPlane, "label");
			_planeRef.push( { face:_frontContainer, plane:_labelPlane } );
			
			for each(var dobj:Object in _defobj.modules) {
				dobj.baseRenderer = "CatNavItem";
				dobj.labelRenderer = "CatNavItemLabel";
			}
			
			
			var pl:Plane = new Plane(_basemat, _basemovie.width, _basemovie.height, 6, 6);
			_frontContainer.addChild(pl, "front");
			_planeRef.push( { face:_frontContainer, plane:pl } );
			
			pmenu = new PaperMenu(_defobj.modules, "horizontal", -5, 5, 0, _shownav);
			
			_frontContainer.addChild(pmenu, "pmenu");
			pmenu.z = -60;
			_planeRef.push( { face:_frontContainer, plane:pmenu } );
			
			var mc2:MovieClip = new PlaneBackFace2();
			var mat2:MovieMaterial = new MovieMaterial(mc2, false, false, false);
			mat2.smooth = true;
			mat2.name = "backmat";
			var pl2:Plane = new Plane(mat2, mc2.width, mc2.height, 3, 3);
			_backContainer.addChild(pl2, "back");
			
			_planeRef.push( { face:_backContainer, plane:pl2 } );
			
			_matref.push(mat2);
			
			createBackLabelPlane(_defobj.name, true);
			
			if(_backed){
				showBackLabel();
				//_frontContainer.visible = false;
				introPlayed = false;
				deactivateControls();
			}else{
				activateControls();
			}
						
		}
		
		public function clearAnimations():void {
			_basemovie.clean();
			stopAnimations();
			
		}
		public function stopAnimations():void {
			//_basemat.animated = false;
			_basemovie.stopAnimations();
		}
		public function showCategoryAnimation(linkage:String):void {
			_basemovie.showAnimation(linkage);
		}
		public function showMenuIndex(n:Number):void {
			pmenu.show(n);
			_menuhasshown = true;
		}
		public function showAllMenu():void {
			_basemovie.clean();
			stopAnimations();
			pmenu.showAll();
		}
		public function hideMenu():void {
			pmenu.hideAll();
		}
		override protected function roll3DActions(evt:InteractiveScene3DEvent):void {
			
			var udata:Object = (evt.target as Plane).userData.data;
			
			var pmenuitem:PaperMenuItem  = udata.movie.pmenu;
			var rolled:Boolean = evt.type == InteractiveScene3DEvent.OBJECT_OVER;
			if (rolled) {
				pmenuitem.createInBetween(new CatNavItemRoll());
				TweenMax.to(pmenuitem, .6, { z: -60, ease:Strong.easeInOut} );	
			}else {
				pmenuitem.removeInBetween();
				TweenMax.to(pmenuitem, .6, {z:0, ease:Strong.easeInOut} );
			}			
			
			super.roll3DActions(evt);	
		}
		override public function moveAndFlipBack(addz:Number = 100, addy:Number = 60):void {
			super.moveAndFlipBack(addz, addy);
			_basemovie.clean();
		}
		override public function moveAndFlipForward(remz:Number = 100, remy:Number = 60):void {
			super.moveAndFlipForward(remz, remy);
			if (introPlayed) {
				pmenu.showAll();
				planeView.createReplay(new Replay(), -30);
			}
		}
		override protected function click3DActions(evt:InteractiveScene3DEvent):void {
			if (_menuhasshown) {
				pmenu.showAll();
				introPlayed = true;
				
			}
			super.click3DActions(evt);
		}
		
		public function get baseMovie():MovieClip {
			return _basemovie;
		}
		
		
		
		
	}
	
}